class VolunteerController {

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def login = {
    if (!session.volunteer)
      render(view: 'login')
    else {
      redirect(controller: 'event', action: 'futureEvents')
    }
  }

  def handleLogin = {
    def volunteer = Volunteer.findByEmail(params.email)
    if (volunteer) {
      if (params.password.equals(volunteer.password)) {
        session.volunteer = volunteer
        redirect(controller: 'event', action: 'list')
      } else {
        flash.message = "Invalid login"
        render view: 'login'
      }
    } else {
      flash.message = "User not found for email: ${params.email}"
      render view: 'login'
    }
  }

  def logout = {
    session.invalidate()
    redirect action: 'login'
  }

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [volunteerInstanceList: Volunteer.list(params), volunteerInstanceTotal: Volunteer.count()]
  }

  def show = {
    def volunteerInstance = Volunteer.get(params.id)

    if (!volunteerInstance) {
      flash.message = "Volunteer not found with id ${params.id}"
      redirect(action: list)
    }
    else { return [volunteerInstance: volunteerInstance] }
  }

  def delete = {
    def volunteerInstance = Volunteer.get(params.id)
    if (volunteerInstance) {
      try {
        volunteerInstance.delete()
        flash.message = "Volunteer ${params.id} deleted"
        redirect(action: list)
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Volunteer ${params.id} could not be deleted"
        redirect(action: show, id: params.id)
      }
    }
    else {
      flash.message = "Volunteer not found with id ${params.id}"
      redirect(action: list)
    }
  }

  def edit = {
    if (params.id != null) {
      // user check
      if (session.volunteer?.id as String != params.id) {
        flash.message = "You can only edit yourself"
        redirect(action: list)
        return
      }
    }
    def volunteerInstance = Volunteer.get(session.volunteer.id)

    if (!volunteerInstance) {
      flash.message = "Volunteer not found with id ${session.volunteer.id}"
      redirect(action: list)
    }
    else {
      return [volunteerInstance: volunteerInstance]
    }
  }

  def update = {
    def volunteerInstance = Volunteer.get(session.volunteer.id)
    if (volunteerInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (volunteerInstance.version > version) {

          volunteerInstance.errors.rejectValue("version", "volunteer.optimistic.locking.failure", "Another user has updated this Volunteer while you were editing.")
          render(view: 'edit', model: [volunteerInstance: volunteerInstance])
          return
        }
      }
      volunteerInstance.properties = params
      if (!volunteerInstance.hasErrors() && volunteerInstance.save()) {
        flash.message = "Volunteer ${params.id} updated"
        redirect(action: show, id: volunteerInstance.id)
      }
      else {
        render(view: 'edit', model: [volunteerInstance: volunteerInstance])
      }
    }
    else {
      flash.message = "Volunteer not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def volunteerInstance = new Volunteer()
    volunteerInstance.properties = params
    return ['volunteerInstance': volunteerInstance]
  }

  def save = {
    def volunteerInstance = new Volunteer(params)
    if (!volunteerInstance.hasErrors() && volunteerInstance.save()) {
      flash.message = "Volunteer ${volunteerInstance.id} created"
      redirect(action: show, id: volunteerInstance.id)
    }
    else {
      render(view: 'create', model: [volunteerInstance: volunteerInstance])
    }
  }
}
